package com.jiai.cloud.community.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.base.util.IdGenerate;
import com.jiai.cloud.base.util.SpringUtil;
import com.jiai.cloud.community.entity.TCatalog;
import com.jiai.cloud.community.entity.TResource;
import com.jiai.cloud.community.service.ITCatalogService;
import com.jiai.cloud.community.service.ITResourceService;
import com.jiai.cloud.config.Config;
import com.jiai.cloud.entity.TGoods;
import com.jiai.cloud.entity.TPageModule;
import com.jiai.cloud.utils.UploadUtil;
import com.sun.xml.internal.xsom.impl.ListSimpleTypeImpl;
import javafx.scene.shape.PathElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

/**
 * Created by Administrator on 2017/6/23.
 */
@RestController
@RequestMapping("/api/v1/TCatalog")
public class TCatalogController {
    @Autowired
    private ITCatalogService itCatalogService;
    @Autowired
    private ITResourceService itResourceService;

    //创建目录
    @RequestMapping(value = "/catalog",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public  Object  newFolder(TCatalog tCatalog)  {
        String catalog=tCatalog.getCatalog();
        String userId=tCatalog.getUserId();
        TCatalog str=itCatalogService.selectOne(new EntityWrapper<TCatalog>().eq("catalog",catalog).eq("userId",userId));
        if ( str== null){
            String  filePath  =IdGenerate.generateShortUuid();
            String filePath2 = SpringUtil.getBean(Config.class).getFilePath();
            String path=filePath2+"\\"+tCatalog.getUserId()+"\\"+filePath;
            File file=new File(path);
            if (!file.exists()){
                file.mkdir();
            }
            tCatalog.setSaveCatalog(filePath);
            itCatalogService.insert(tCatalog);
            return RestResultGenerator.genSuccessResult(tCatalog);
        }else {
            return  RestResultGenerator.genErrorResult("分组已经存在");
        }
    }
    //查所有
    @RequestMapping(value = "/findByAll",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findByAll(String userId) throws ServiceException {
        try {
        String filePath = SpringUtil.getBean(Config.class).getFilePath();//获取当去路径；
        String  relativelyPath=filePath+"\\"+userId;
        File  file=new File(relativelyPath);//创建当前默认路径
                  if (file.exists()){
                      return RestResultGenerator.genSuccessResult( itCatalogService.selectList(new  EntityWrapper<TCatalog>().eq("userId",userId)));
                  }else {
                      file.mkdir();
                      return  RestResultGenerator.genSuccessResult( itCatalogService.selectList(new  EntityWrapper<TCatalog>().eq("userId",userId)));
                  }
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }


    //修改分组
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit( TCatalog tCatalog){
       int count= itCatalogService.selectCount(new EntityWrapper().eq("userId",tCatalog.getUserId()).eq("catalog",tCatalog.getCatalog()));
       if(count>0){
           return  RestResultGenerator.genErrorResult("修改标签失败，可能是标签名重复，请检查");
       }else {
           itCatalogService.updateById(tCatalog);
           return RestResultGenerator.genSuccessResult(tCatalog);
       }
    }
    //删除分类
    @RequestMapping(value = "/del", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object deleteId( Integer catalogId) throws ServiceException{
        try{
            TCatalog tCatalog1=itCatalogService.selectById(catalogId);//获取当前图片保存分组
            String filePath = SpringUtil.getBean(Config.class).getFilePath(); //获取当前所有根路径
            String path1=filePath+"\\"+tCatalog1.getUserId()+"\\"+tCatalog1.getSaveCatalog();
            String path2=filePath+"\\"+tCatalog1.getUserId();// 获取当前默认路径
            UploadUtil.IOCopy(path1,path2);
            TResource tResource=new TResource();
            tResource.setCatalogId(0);
            itResourceService.update(tResource,new EntityWrapper<TResource>().eq("catalogId",catalogId).eq("userId",tCatalog1.getUserId()));
            itCatalogService.deleteById(catalogId);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
    //
}
